cd $SRC
validpgpkeys=('14F26682D0916CDD81E37B6D61B7B526D98F0353') 
_google_api_key=AIzaSyDwr302FpOSkGRpLlUpPThNTDPbXcIn_FM
_mozilla_api_key=e05d56db0a694edc8b5aaebda3f2db6a

mkdir mozbuild
  cd firefox-$surum

  # https://bugzilla.mozilla.org/show_bug.cgi?id=1530052
  patch -Np1 -i /sources/0001-Use-remoting-name-for-GDK-application-names.patch

  # https://bugzilla.mozilla.org/show_bug.cgi?id=1704022
  patch -Np1 -i /sources/0002-Bug-1701089-Accept-vp09-codec-string-in-Widevine-man.patch

  echo -n "$_google_api_key" >google-api-key
  echo -n "$_mozilla_api_key" >mozilla-api-key

  cat >../mozconfig <<END
ac_add_options --enable-application=browser
mk_add_options MOZ_OBJDIR=${PWD@Q}/obj
ac_add_options --prefix=/usr
ac_add_options --enable-release
ac_add_options --enable-hardening
ac_add_options --enable-optimize
ac_add_options --enable-rust-simd
ac_add_options --enable-linker=lld
ac_add_options --disable-elf-hack
# Branding
ac_add_options --enable-official-branding
ac_add_options --enable-update-channel=release
ac_add_options --with-distribution-id=org.aylinux
ac_add_options --with-unsigned-addon-scopes=app,system
ac_add_options --allow-addon-sideload
export MOZILLA_OFFICIAL=1
export MOZ_APP_REMOTINGNAME=${pkgname//-/}
export MOZ_TELEMETRY_REPORTING=1
export MOZ_REQUIRE_SIGNING=1
# Keys
ac_add_options --with-google-location-service-api-keyfile=${PWD@Q}/google-api-key
ac_add_options --with-google-safebrowsing-api-keyfile=${PWD@Q}/google-api-key
ac_add_options --with-mozilla-api-keyfile=${PWD@Q}/mozilla-api-key
# System libraries
ac_add_options --with-system-nspr
ac_add_options --with-system-nss
# Features
ac_add_options --enable-alsa
ac_add_options --enable-jack
ac_add_options --enable-crashreporter
ac_add_options --disable-updater
ac_add_options --disable-tests
END



  export MOZ_NOSPAM=1
  export MOZBUILD_STATE_PATH="$SRC/mozbuild"
  export MOZ_ENABLE_FULL_SYMBOLS=1
  export MACH_USE_SYSTEM_PYTHON=1

  # LTO needs more open files
  ulimit -n 4096

  # Do 3-tier PGO
  echo "Building instrumented browser..."
  cat >.mozconfig ../mozconfig - <<END
ac_add_options --enable-profile-generate=cross
END
  ./mach build

  echo "Profiling instrumented browser..."
  ./mach package
  LLVM_PROFDATA=llvm-profdata \
    JARLOG_FILE="$PWD/jarlog" \
    xvfb-run -s "-screen 0 1920x1080x24 -nolisten local" \
    ./mach python build/pgo/profileserver.py

  stat -c "Profile data found (%s bytes)" merged.profdata
  test -s merged.profdata

  stat -c "Jar log found (%s bytes)" jarlog
  test -s jarlog

  echo "Removing instrumented browser..."
  ./mach clobber

  echo "Building optimized browser..."
  cat >.mozconfig ../mozconfig - <<END
ac_add_options --enable-lto=cross
ac_add_options --enable-profile-use=cross
ac_add_options --with-pgo-profile-path=${PWD@Q}/merged.profdata
ac_add_options --with-pgo-jarlog=${PWD@Q}/jarlog
END
  ./mach build

  echo "Building symbol archive..."
  ./mach buildsymbols


  DESTDIR="$PKG" ./mach install

  local vendorjs="$PKG/usr/lib/$isim/browser/defaults/preferences/vendor.js"
  install -Dvm644 /dev/stdin "$vendorjs" <<END
// Use LANG environment variable to choose locale
pref("intl.locale.requested", "");
// Use system-provided dictionaries
pref("spellchecker.dictionary_path", "/usr/share/hunspell");
// Disable default browser checking.
pref("browser.shell.checkDefaultBrowser", false);
// Anasayfa.
pref("browser.startup.homepage", "http://www.aylinux.org/");
// arama.
pref("browser.urlbar.placeholderName", "Google");
// arama.
pref("browser.urlbar.placeholderName.private", "Google");
// Don't disable extensions in the application directory
pref("extensions.autoDisableScopes", 11);
END

  local distini="$PKG/usr/lib/$pkgname/distribution/distribution.ini"
  install -Dvm644 /dev/stdin "$distini" <<END
[Global]
id=aylinux
version=1.0
about=Aylinux için firefox
[Preferences]
app.distributor=aylinux
app.distributor.channel=$isim
app.partner.aylinux=aylinux
END

  local i theme=official
  for i in 16 22 24 32 48 64 128 256; do
    install -Dvm644 browser/branding/$theme/default$i.png \
      "$PKG/usr/share/icons/hicolor/${i}x${i}/apps/$isim.png"
  done
  install -Dvm644 browser/branding/$theme/content/about-logo.png \
    "$PKG/usr/share/icons/hicolor/192x192/apps/$isim.png"
  install -Dvm644 browser/branding/$theme/content/about-logo@2x.png \
    "$PKG/usr/share/icons/hicolor/384x384/apps/$isim.png"
  install -Dvm644 browser/branding/$theme/content/identity-icons-brand.svg \
    "$PKG/usr/share/icons/hicolor/symbolic/apps/$isim-symbolic.svg"

  install -Dvm644 ../$isim.desktop \
    "$PKG/usr/share/applications/$isim.desktop"

  # Install a wrapper to avoid confusion about binary path
  install -Dvm755 /dev/stdin "$PKG/usr/bin/$isim" <<END
#!/bin/sh
exec /usr/lib/$isim/firefox "\$@"
END

  # Replace duplicate binary with wrapper
  # https://bugzilla.mozilla.org/show_bug.cgi?id=658850
  ln -srfv "$PKG/usr/bin/$isim" "$PKG/usr/lib/$isim/firefox-bin"

  # Use system certificates
  local nssckbi="$PKG/usr/lib/$isim/libnssckbi.so"
  if [[ -e $nssckbi ]]; then
    ln -srfv "$PKG/usr/lib/libnssckbi.so" "$nssckbi"
  fi

  export SOCORRO_SYMBOL_UPLOAD_TOKEN_FILE="$startdir/.crash-stats-api.token"
  if [[ -f $SOCORRO_SYMBOL_UPLOAD_TOKEN_FILE ]]; then
    make -C obj uploadsymbols
  else
    cp -fvt "$startdir" obj/dist/*crashreporter-symbols-full.tar.zst
  fi